package com.example.liumoxiangmu;

import androidx.appcompat.app.AppCompatActivity;

import android.nfc.Tag;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Objects;

import cn.com.newland.nle_sdk.requestEntity.SignIn;
import cn.com.newland.nle_sdk.responseEntity.SensorInfo;
import cn.com.newland.nle_sdk.responseEntity.User;
import cn.com.newland.nle_sdk.responseEntity.base.BaseResponseEntity;
import cn.com.newland.nle_sdk.util.NCallBack;
import cn.com.newland.nle_sdk.util.NetWorkBusiness;

public class MainActivity extends AppCompatActivity {
    NetWorkBusiness netWorkBusiness;
    String Url = "http://api.nlecloud.com",Id = "929166";
    TextView text_temp,text_hum,text_smoke,text_ala,text_fan,text_lamp,text_curtain,text_wifi;
    TextView txt_lamp,txt_fan,txt_curtain,txt_ala,txt_wifi;
    Switch s_lamp,s_fan,s_curtain,s_ala,s_wifi;
    Boolean lamp_onoff = false,fan_onoff = false, curtain_onoff = false,ala_onoff = false,wifi_onoff = false;
    Boolean smoke_celue = true;
    String Temp = "0.0",Hum,Smoke,Value;
    LinearLayout l_lamp,l_fan,l_curtain,l_ala,l_wifi;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Init();


        new NetWorkBusiness("445fcd9128b04aee83b48428e2065eb8",Url)
                .signIn(new SignIn("13680163531", "Wu_Lian_Wang"), new NCallBack<BaseResponseEntity<User>>(getApplicationContext()) {
                    @Override
                    protected void onResponse(BaseResponseEntity<User> userBaseResponseEntity) {
                        netWorkBusiness = new NetWorkBusiness(userBaseResponseEntity.getResultObj().getAccessToken(),Url);
                        Value();
                        s_lamp.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                lamp_onoff = !lamp_onoff;
                                if(lamp_onoff){
                                    text_lamp.setBackgroundResource(R.drawable.lamp_on);
                                    l_lamp.setBackgroundResource(R.drawable.black_card);
                                    txt_lamp.setTextColor(0xFFFFFFFF);
                                    control("m_lamp",1);

                                }else{
                                    text_lamp.setBackgroundResource(R.drawable.lamp_off);
                                    l_lamp.setBackgroundResource(R.drawable.control_card);
                                    txt_lamp.setTextColor(0xFF000000);
                                    control("m_lamp",0);
                                }

                            }
                        });
                        s_fan.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                fan_onoff = !fan_onoff;

                                if (fan_onoff){
                                    text_fan.setBackgroundResource(R.drawable.fan_on);
                                    txt_fan.setTextColor(0xFFFFFFFF);
                                    l_fan.setBackgroundResource(R.drawable.black_card);
                                    control("m_fan",1);
                                }else{
                                    text_fan.setBackgroundResource(R.drawable.fan_off);
                                    txt_fan.setTextColor(0xFF000000);
                                    l_fan.setBackgroundResource(R.drawable.control_card);
                                    control("m_fan",0);
                                }
                            }
                        });
                        s_curtain.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                curtain_onoff = !curtain_onoff;

                                if (curtain_onoff){
                                    text_curtain.setBackgroundResource(R.drawable.curtain_on);
                                    txt_curtain.setTextColor(0xFFFFFFFF);
                                    l_curtain.setBackgroundResource(R.drawable.black_card);
                                    control("m_ele_push",0);
                                    try{
                                        Thread.sleep(500);
                                    }catch (Exception e){
                                        e.printStackTrace();
                                    }
                                    control("m_ele_shrink",1);
                                }else{
                                    text_curtain.setBackgroundResource(R.drawable.curtain_off);
                                    txt_curtain.setTextColor(0xFF000000);
                                    l_curtain.setBackgroundResource(R.drawable.control_card);

                                    control("m_ele_shrink",0);
                                    try{
                                        Thread.sleep(500);
                                    }catch (Exception e){
                                        e.printStackTrace();
                                    }
                                    control("m_ele_push",1);
                                }
                            }
                        });
                        s_ala.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                ala_onoff = !ala_onoff;
                                smoke_celue = !smoke_celue;

                                if (ala_onoff){
                                    text_ala.setBackgroundResource(R.drawable.ala_on);
                                    txt_ala.setTextColor(0xFF000000);
                                    l_ala.setBackgroundResource(R.drawable.black_card);
                                    control("m_alarmlight",1);
                                }else{
                                    text_ala.setBackgroundResource(R.drawable.ala_off);
                                    txt_ala.setTextColor(0xFFFFFFFF);
                                    l_ala.setBackgroundResource(R.drawable.control_card);
                                    control("m_alarmlight",0);
                                }

                                if (smoke_celue){
                                    Toast.makeText(MainActivity.this, "打开烟雾报警策略", Toast.LENGTH_SHORT).show();
                                }else{
                                    Toast.makeText(MainActivity.this, "关闭烟雾报警策略", Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
                        s_wifi.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                wifi_onoff = !wifi_onoff;
                                if (wifi_onoff){
                                    text_wifi.setBackgroundResource(R.drawable.wf1);
                                    txt_wifi.setTextColor(0xFFFFFFFF);
                                    l_wifi.setBackgroundResource(R.drawable.black_card);
                                }else{
                                    text_wifi.setBackgroundResource(R.drawable.wf);
                                    txt_wifi.setTextColor(0xFF000000);
                                    l_wifi.setBackgroundResource(R.drawable.control_card);
                                }
                            }
                        });
                    }
                });
    }

    private void Value(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    netWorkBusiness.getSensor(Id, "z_temperature", new NCallBack<BaseResponseEntity<SensorInfo>>(getApplicationContext()) {
                        @Override
                        protected void onResponse(BaseResponseEntity<SensorInfo> response) {
                            Temp = response.getResultObj().getValue();
                            Log.d(Temp,"温度");
                            text_temp.setText(Temp+"℃");
                        }
                    });

                    netWorkBusiness.getSensor(Id, "z_humidity", new NCallBack<BaseResponseEntity<SensorInfo>>(getApplicationContext()) {
                        @Override
                        protected void onResponse(BaseResponseEntity<SensorInfo> response) {
                            Hum = response.getResultObj().getValue();
                            Log.d(Hum,"湿度");
                            text_hum.setText(Hum+"%rh");
                        }
                    });

//                    netWorkBusiness.getSensor(Id, "m_smoke", new NCallBack<BaseResponseEntity<SensorInfo>>(getApplicationContext()) {
//                        @Override
//                        protected void onResponse(BaseResponseEntity<SensorInfo> response) {
//                            Smoke = response.getResultObj().getValue();
//                            Log.d(Smoke,"a");
//
//                            if (smoke_celue){ // 打开策略控制报警灯
//                                if(Objects.equals(Smoke, "1")){
//                                    ala_onoff = true;
//                                    text_smoke.setText("异常");
//                                    text_smoke.setTextColor(0xFFFF0000);
//                                    text_ala.setBackgroundResource(R.drawable.ala_on);
//                                    txt_ala.setTextColor(0xFFFFFFFF);
//                                    l_ala.setBackgroundResource(R.drawable.black_card);
//                                }else{
//                                    ala_onoff = false;
//                                    text_smoke.setText("正常");
//                                    text_smoke.setTextColor(0xFFFFFFFF);
//                                    text_ala.setBackgroundResource(R.drawable.ala_off);
//                                    txt_ala.setTextColor(0xFF000000);
//                                    l_ala.setBackgroundResource(R.drawable.control_card);
//                                }
//                                s_ala.setChecked(ala_onoff);
//                                control("m_alarmlight",ala_onoff);
//                            }else {//关闭策略不控制报警灯，只控有害气体浓度是否正常
//                                if(Objects.equals(Smoke, "1")){
//                                    ala_onoff = true;
//                                    text_smoke.setText("异常");
//                                    text_smoke.setTextColor(0xFFFF0000);
//                                }else{
//                                    ala_onoff = false;
//                                    text_smoke.setText("正常");
//                                    text_smoke.setTextColor(0xFFFFFFFF);
//                                }
//                            }
//
//                        }
//                    });
                    try{
                        Thread.sleep(1000);
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }


            }
        }).start();
    }


    public void control(String Tag,final int value){
        netWorkBusiness.control(Id, Tag, value, new NCallBack<BaseResponseEntity>(getApplicationContext()) {
            @Override
            protected void onResponse(BaseResponseEntity baseResponseEntity) {

            }
        });
    }
    private void Init(){
        text_temp = findViewById(R.id.t_temp);
        text_hum = findViewById(R.id.t_hum);
        text_smoke = findViewById(R.id.t_smoke);
        text_ala = findViewById(R.id.t_ala);
        text_fan = findViewById(R.id.t_fan);
        text_lamp = findViewById(R.id.t_lamp);
        text_curtain = findViewById(R.id.t_curtain);
        text_wifi = findViewById(R.id.t_wifi);
        txt_lamp = findViewById(R.id.txt_lamp);
        txt_fan = findViewById(R.id.txt_fan);
        txt_curtain = findViewById(R.id.txt_curtain);
        txt_ala = findViewById(R.id.txt_ala);
        txt_wifi = findViewById(R.id.txt_wifi);
        s_lamp = findViewById(R.id.s_lamp);
        s_fan = findViewById(R.id.s_fan);
        s_curtain = findViewById(R.id.s_curtain);
        s_ala = findViewById(R.id.s_ala);
        s_wifi = findViewById(R.id.s_wifi);
        l_lamp = findViewById(R.id.layout_lamp);
        l_fan = findViewById(R.id.layout_fan);
        l_curtain = findViewById(R.id.layout_cutain);
        l_ala = findViewById(R.id.layout_ala);
        l_wifi = findViewById(R.id.layout_wifi);
    }
}